
package com.taillive.commons.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.net.URL;
import java.util.*;

import com.taillive.commons.WebUtils;



public class TextInput {

    

    /**
     *   Read text content from a text file.
     *   @param fileName name of the text file.
     *   @return text content of a text file.
     */
    public static String readContent(String fileName) {
    	return readContent(fileName,false);
    }


    /**
     *   Read text content from a text file.
     *   @param fileName name of the text file.
     *   @return text content of a text file.
     */
    public static String readContent(String fileName, boolean withLineBreak) {


        StringBuffer sb = new StringBuffer();
        InputStream in = null;
        URL url = null;
        BufferedReader reader = null;
        try {
            if (WebUtils.isURLFile(fileName)) {
            	url = new URL(fileName);
                in = url.openStream();

            }
            else {
                in = new FileInputStream(fileName);

            }

            reader = new BufferedReader(new InputStreamReader(in));
            while (true) {
                String text = reader.readLine();
                if (text == null) {
                    break;
                }
                sb.append(text);

                if (withLineBreak)
                	sb.append("\n");
            }




            //for performance/memory leak reason, do not return buf.toString immediately:
            String content = new String(sb.toString());
            sb.setLength(0);
            sb = null;
            return content;

        }
        catch (Exception e) {
        	e.printStackTrace();
            return null;
        }finally{
            try {
            	if(reader!=null){
            		reader.close();
            	}
            	if(in!=null){
            		in.close(); 
            	}
			} catch (IOException e) {
				e.printStackTrace();
			}
                   	
            url = null;
            reader = null;
            in = null;  
            
        }
    }




    /**
     *   Read text content from a text file.
     *   @param fileName name of the text file.
     *   @return a list for all text lines
     */
    public static List readContentList(String fileName) {

		List<String> lines	=new ArrayList<String>();
		BufferedReader reader = null ;
		URL url = null;
		InputStream in = null;
        try {
            if (WebUtils.isURLFile(fileName)) {
                url = new URL(fileName);
                in = url.openStream();

            }
            else {
                in = new FileInputStream(fileName);

            }

            reader = new BufferedReader(new InputStreamReader(in));
            while (true) {
                String text = reader.readLine();
                if (text == null) {
                    break;
                }
                lines.add(text);
            }

           

        }
        catch (Exception e) {
            System.out.println("read text file error: " + e.getMessage());
           
        }
        finally{
            try {
            	if(reader!=null){
            		reader.close();
            	}
            	if(in!=null){
            		in.close(); 
            	}
			} catch (IOException e) {
				e.printStackTrace();
			}
                   	
            url = null;
            reader = null;
            in = null;  
            
        }
        
        return lines;
    }

}
